<template>
  <el-dialog
  title="新增学科"
  :visible="showDialog"
  width="30%"
  @close="onClose">
  <el-form :model="form" ref="formRef"  label-width="80px" :rules="formRules">
    <el-form-item label="学科名称" prop="subjectName">
      <el-input size="medium" v-model="form.subjectName" placeholder="请输入学科名称"></el-input>
    </el-form-item>
    <el-form-item label="是否开启" prop="isFrontDisplay">
      <el-switch v-model="form.isFrontDisplay" active-color="#13ce66" inactive-color="#ff4949"></el-switch>
    </el-form-item>
  </el-form>
  <span slot="footer" class="dialog-footer">
    <el-button size="mini" @click="onClose">取 消</el-button>
    <el-button size="mini" type="primary" @click="onConfirm">确 定</el-button>
  </span>
</el-dialog>
</template>
<script>
import { add } from '@/api//hmmm/subjects.js'
export default {
  name: 'AddSubject',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      form: {
        // 学科名称
        subjectName: '',
        // 是否开启
        isFrontDisplay: true
      },
      formRules: {
        subjectName: [
          { required: true, message: '请输入学科名称', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 关闭对话框
    onClose () {
      this.$refs.formRef.resetFields()
      this.$emit('update:showDialog', false)
    },
    // 确认
    onConfirm () {
      this.$refs.formRef.validate(async (valid) => {
        if (!valid) return
        await add(this.form)
        this.$message.success('添加成功')
        this.$emit('update:showDialog', false)
        this.$emit('reload')
      })
    }
  }
}
</script>
<style lang="scss">
</style>
